# -*- coding: utf-8 -*-
import time

import scrapy

from cctvsix.items import FilmItem


class CctvfilmSpider(scrapy.Spider):
    name = 'cctvfilm'
    allowed_domains = ['www.1905.com']
    # 拼接所有页面链接，页面总页码为115页
    start_urls = [f'https://www.1905.com/cctv6/all/film/list/w_1/o1l0p{i}.html' for i in range(1, 116)]

    def parse(self, response):
        film_urls = response.css(".img::attr(href)").extract()  # 获取当前页面中的所有影片资源
        for film_url in film_urls:
            yield scrapy.Request(url=film_url, callback=self.parse_page)

    def parse_page(self, response):
        time.sleep(5)
        try:
            film_item = FilmItem()
            film_item["title"] = response.css(".container-right h1::text").extract_first().strip()  # 片名
            film_item["creator"] = response.css(".creator-name::text").extract_first().strip()  # 导演
            info_href_items = response.css(".information-item a::text")
            info_text_items = response.css(".information-item::text")
            re_up_time = info_text_items.re("\d+-\d+-\d+")  # 上线时间
            if len(re_up_time) <= 0:
                film_item["up_time"] = None
            else:
                film_item["up_time"] = re_up_time[0]
            re_duration = info_text_items.re("\d+小时\d+分钟")  # 影片时长
            if len(re_duration) <= 0:
                film_item["duration"] = None
            else:
                film_item["duration"] = re_duration[0]
            re_region = info_href_items.re("中国香港|中国|美国|加拿大|法国|德国|英国")  # 地区
            if len(re_region) <= 0:
                film_item["region"] = None
            else:
                film_item["region"] = re_region[0]
            film_item["report_num"] = response.css(".total::text").extract_first()  # 补充字段：相关报道数
            re_video_img = response.css(".mod-title .thin::text").re(r"\d+")
            if len(re_video_img) <= 0:
                film_item["video_num"] = None
                film_item["img_num"] = None
            elif len(re_video_img) == 1:
                film_item["video_num"] = re_video_img[0]
                film_item["img_num"] = None
            else:
                film_item["video_num"] = response.css(".mod-title .thin::text").re(r"\d+")[0]  # 补充字段：相关视频数
                film_item["img_num"] = response.css(".mod-title .thin::text").re(r"\d+")[1]  # 补充字段：相关图片数
            f = response.css(".score b::text").extract_first()
            l = response.css(".score::text").extract_first()
            if f is not None and l is not None:
                film_item["score"] = f + l  # 评分
            film_item["evaluate_num"] = response.css("#evaluateNum::text").extract_first()  # 参与评分的人数
            yield film_item
        except Exception as e:
            print(e)
